package com.bosssoft.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.entity.dto.FibIndicatorsDTO;
import com.bosssoft.entity.dto.FibNewsDTO;
import com.bosssoft.entity.vo.FibIndicatorsVO;
import com.bosssoft.entity.vo.FibNewsVO;
import com.bosssoft.page.PageResult;
import com.bosssoft.response.Result;
import com.bosssoft.service.FibIndicatorsService;
import com.bosssoft.service.FibNewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.bosssoft.enums.BgtCode.NOT_SERPPORT;

/**
 * @author Li kexi
 * Created on 2021/8/20.9:51
 */
@RestController
@Api(tags = "指标管理控制器")
@RequestMapping("/indicator")
@Slf4j
public class IndicatorsController {

    /**
     * 一、全部查询
     * 二、按状态查询分类（未送审，已送审，已作废）
     * 三、指标录入与更新
     * 四、审核
     * 五、退回（附加退回意见）
     * 六、指标分配（一开始的创建就是一次分配）
     */

    @Autowired
    private FibIndicatorsService fibIndicatorsService;

    @Autowired
    private FibNewsService fibNewsService;

    @PostMapping("/queryAll")
    @ApiOperation("全部查询分页查")
    public Result queryAll(@RequestParam(defaultValue = "1") Integer pageNo,
                           @RequestParam(defaultValue = "10") Integer pageSize,FibIndicatorsVO fibIndicatorsVO) {

        Page<FibIndicatorsVO> page = fibIndicatorsService.selectAllByPage(fibIndicatorsVO,pageNo,pageSize);
        PageResult<FibIndicatorsVO> pageResult = new PageResult<>(page, page.getRecords());
        return Result.ok().data("page", pageResult);
    }

    @PostMapping("/queryByStatus")
    @ApiOperation("按状态分页查")
    public Result queryByStatus(@RequestParam(defaultValue = "1") Integer pageNo,
                                @RequestParam(defaultValue = "10") Integer pageSize,
                                Integer status) {
        Page<FibIndicatorsVO> page = fibIndicatorsService.selectByStatus(pageNo,pageSize, status);
        PageResult<FibIndicatorsVO> pageResult = new PageResult<>(page, page.getRecords());
        return Result.ok().data("page", pageResult);
    }

    @PostMapping("/queryByType")
    @ApiOperation("根据指标类型分页查询")
    //指标类型分为处室指标、单位指标，int类型
    public Result queryByType(@RequestParam(defaultValue = "1") Integer pageNo,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              Integer IndType) {
        Page<FibIndicatorsVO> page = fibIndicatorsService.selectByType(pageNo,pageSize,IndType);
        PageResult<FibIndicatorsVO> pageResult = new PageResult<>(page, page.getRecords());
        return Result.ok().data("page", pageResult);
    }

    @PostMapping("/input")
    @ApiOperation("指标录入与更新")
    //针对处室指标才有录入，针对单位指标采取分配方式录入
    public Result inputIndicators(@RequestBody @Validated FibIndicatorsVO request) {

        if (request.getIndType() == 2) {
            return Result.error(NOT_SERPPORT);
        }
        FibIndicatorsDTO fibIndicatorsDTO = new FibIndicatorsDTO();
        BeanUtils.copyProperties(request, fibIndicatorsDTO);
        int save = fibIndicatorsService.inputIndicators(fibIndicatorsDTO);
        if (save == 0) {
            log.info("录入失败");
            return Result.error();
        }
        return Result.ok();
    }

    @PostMapping("/audit")
    @ApiOperation("审核指标")
    public Result audit(Long id, Integer status, FibNewsVO newsVO) {

        if (status == 3) {
            //审核退回
            FibNewsDTO newsDTO = new FibNewsDTO();
            BeanUtils.copyProperties(newsVO, newsDTO);
            fibNewsService.sendNews(newsDTO);
            boolean isOk = fibIndicatorsService.auditReject(id, status);
        } else {
            //审核通过
            boolean isOk = fibIndicatorsService.auditPass(id, status);
        }
        return Result.ok();
    }

    @PostMapping("/distribution")
    @ApiOperation("指标分配")
    // 此操作仅支持审核已经 通过的 处室类型指标，也就是从一个指标 新建指标 的的方式
    public Result distribution(@RequestBody @Validated FibIndicatorsVO request) {

        // 传入的参数是新的指标记录
        FibIndicatorsDTO indicatorsDTO = new FibIndicatorsDTO();
        BeanUtils.copyProperties(request, indicatorsDTO);
        boolean input = fibIndicatorsService.distribution(indicatorsDTO);
        if (!input) {
            log.info("分配新的指标失败");
            return Result.error();
        }
        log.info("分配指标成功");
        return Result.ok();
    }
}

